package cn.net.zj.scan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.maning.mndialoglibrary.MProgressDialog;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.net.zj.scan.R;
import cn.net.zj.scan.adapter.TaskListAdapter;
import cn.net.zj.scan.api.HttpMethods;
import cn.net.zj.scan.base.CommonActivity;
import cn.net.zj.scan.base.Result;
import cn.net.zj.scan.bean.TaskBean;
import cn.net.zj.scan.utils.MProgressDialogHelper;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class TaskListActivity extends CommonActivity {
    //<editor-fold desc=”成员变量”>


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private TaskListAdapter taskListAdapter;

    //</editor-fold>

    //<editor-fold desc=”生命周期”>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_list);
        ButterKnife.bind(this);

        configUI();
    }
    //</editor-fold>


    //<editor-fold desc=”私有方法”>
    private void configUI() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        taskListAdapter = new TaskListAdapter(R.layout.item_task, null);
        taskListAdapter.bindToRecyclerView(recyclerView);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        taskListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TaskBean item = (TaskBean) adapter.getItem(position);
                Intent intent = new Intent(TaskListActivity.this, TaskDetailListActivity.class);
                intent.putExtra("FBillNo", item.getFBillNo());
                ActivityUtils.startActivity(intent);
            }
        });

        requestTaskList();
    }
    //</editor-fold>
    //<editor-fold desc=”网络请求”>

    private void requestTaskList() {


        String r_icmobill = getIntent().getStringExtra("r_icmobill");
        String start_time = getIntent().getStringExtra("start_time");
        String end_time = getIntent().getStringExtra("end_time");
        String ftype = getIntent().getStringExtra("ftype");

        MProgressDialogHelper.showLoading(this);


        HashMap<String, String> params = new HashMap<>();
        if (!StringUtils.isEmpty(start_time)) {
            params.put("r_icmobill", r_icmobill);
        }
        params.put("start_time", start_time);
        params.put("end_time", end_time);
        params.put("ftype", ftype);

        HttpMethods.getInstance().getFyApi().getTaskList(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<Result<ArrayList<TaskBean>>>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onSuccess(Result<ArrayList<TaskBean>> value) {
                        MProgressDialog.dismissProgress();
                        if (value.getData() != null && value.getData().size() > 0) {
                            taskListAdapter.setNewData(value.getData());
                        } else {
                            ToastUtils.showLong(value.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        MProgressDialog.dismissProgress();
                        ToastUtils.showLong(e.getMessage());
                    }
                });


    }
    //</editor-fold>


}